#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <math.h>
#include <algorithm>
using namespace std;

vector<vector<int>> threeSum(vector<int>& nums) {
        vector<int>three(3,0);
        vector<vector<int>>result;
        int size=nums.size(),k=0;
        for(int i=0;i<=size-3;i++){
            for(int j=i+1;j<=size-2;j++){
                for(int k=j+1;k<=size-1;k++){
                    if(nums[i]+nums[j]+nums[k]==0){
                     three[0]=nums[i];
                     three[1]=nums[j];
                     three[2]=nums[k];
                     sort(three.begin(),three.end());
                     for(int m=0;m<result.size();m++){
                         if(three[0]==result[m][0]&&
                         three[1]==result[m][1]&&
                         three[2]==result[m][2]){
                            goto A;
                         }
                     }
                     result.push_back(three);
                     A:;
                    }
                }
            }
        }
    return result;
}

vector<vector<int>> threeSum1(vector<int>& nums) {
    vector<int>three(3,0);
    vector<vector<int>>result;
    sort(nums.begin(),nums.end());
    for(auto temp:nums){
        cout<<temp<<",";
    }
    cout<<endl;

    int size=nums.size();
    int k,j;
    for(int i=0;i<=size-3;i++){
        if(i!=0&&nums[i]==nums[i-1])continue;
        j=i+1;k=size-1;
            while(j<k){
                    if(nums[j]+nums[k]==-nums[i]){
                        three[0]=nums[i];
                        three[1]=nums[j];
                        three[2]=nums[k];
                        result.push_back(three);
                        j++;k--;
                        while((nums[j]==nums[j-1]||nums[k]==nums[k+1])&&
                        j<k){
                            if(nums[j]==nums[j-1])j++;
                            if(nums[k]==nums[k+1])k--;
                        }
                    }
                    else if(nums[j]+nums[k]<-nums[i]){
                        j++;
                    }else if(nums[j]+nums[k]>-nums[i]){
                        k--;
                    }
            }
    }
    return result;
}

int main(){
//    vector<int>a={-1,0,1,2,-1,-4};
//    vector<int>a={-2,0,0,2,2};
    vector<int>a={0,0,0};
    vector<vector<int>>result=threeSum1(a);
    for(int i=0;i<a.size();i++){
        cout<<result[i][0]<<","<<result[i][1]<<","<<result[i][2]<<endl;
    }
    return 0;
}